﻿using UnityEngine;
using System.Collections;
using CHFramework;

public class AnimationManager : MonoBehaviour
{
    public InputHandler inputHandler;

    private Animation playerAnimation;

	private void Start () 
	{
        inputHandler = PlayRun;
        playerAnimation = GetComponent<Animation>();
    }
	
	private void Update ()
	{
        if (inputHandler != null)
            inputHandler();
	}

    /// <summary>
    /// 播放跑的动画
    /// </summary>
    public void PlayRun()
    {
        PlayAniamtion("Run");
    }

    /// <summary>
    /// 播放死亡动画
    /// </summary>
    public void PlayDead()
    {
        PlayAniamtion("Dead");
    }

    /// <summary>
    /// 播放跳起下落时的动画
    /// </summary>
    public void PlayJumpDown()
    {
        PlayAniamtion("JumpDown");
    }

    /// <summary>
    /// 播放跳起在空中的动画
    /// </summary>
    public void PlayJumpLoop()
    {
        PlayAniamtion("JumpLoop");
    }

    /// <summary>
    /// 两连跳
    /// </summary>
    public void PlayDoubleJump()
    {
        PlayAniamtion("Roll", 0.96f, PlayJumpLoop);
    }

    /// <summary>
    /// 播放跳起动画
    /// 在播放到跳起动画长度的96%时播放跑的动画
    /// </summary>
    public void PlayJumpUp()
    {
        PlayAniamtion("JumpUp", 0.96f, PlayRun);
    }

    /// <summary>
    /// 播放滚动动画
    /// 在播放到滚动动画长度的96%时播放跑的动画
    /// </summary>
    public void PlayRoll()
    {
        PlayAniamtion("Roll", 0.96f, PlayRun);
    }

    /// <summary>
    /// 播放向左跳动画
    /// 在播放到向左跳动画长度的96%时播放跑的动画
    /// </summary>
    public void PlayTurnLeft()
    {
        PlayAniamtion("TurnLeft", 0.96f, PlayRun);
    }

    /// <summary>
    /// 播放向右跳动画
    /// 在播放到向右跳动画长度的96%时播放跑的动画
    /// </summary>
    public void PlayTurnRight()
    {
        PlayAniamtion("TurnRight", 0.96f, PlayRun);
    }

    /// <summary>
    /// 播放动画
    /// </summary>
    /// <param name="aniName"></param>
    /// <param name="duration"></param>
    /// <param name="handler"></param>
    private void PlayAniamtion(string aniName, float duration = 0.95f, InputHandler handler = null)
    {
        playerAnimation.Play(aniName);
        if (playerAnimation[aniName].normalizedTime >= duration)
            inputHandler = handler;
    }
}
